package com.qidian.mes.pro.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qidian.mes.pojo.pro.ProProcess;
import com.qidian.mes.pro.mapper.ProProcessMapper;
import com.qidian.mes.pro.service.ProProcessService;
import com.qidian.mes.util.pojo.MESResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Calendar;
import java.util.List;

/**
 * @Author 郭靖旖
 * @Company
 * @Time 2023/10/30 18:52
 * @Description
 **/
@Service
public class ProProcessServiceImpl implements ProProcessService {
    @Autowired
    ProProcessMapper proProcessMapper;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Override
    public MESResult selectallByPage(int page, int limit) {
        PageHelper.startPage(page,limit);
        List<ProProcess> list=proProcessMapper.selectallByPage();
        PageInfo<ProProcess> pageInfo=new PageInfo<>(list);
        return MESResult.ok(pageInfo);
    }

    @Override
    public MESResult list(int page, int limit, ProProcess proProcess) {
        PageHelper.startPage(page,limit);
        List<ProProcess> processList=proProcessMapper.list(proProcess);
       PageInfo<ProProcess> pageInfo=new PageInfo<>(processList);
        return MESResult.ok(pageInfo);
    }

    @Override
    public MESResult genProcessCode() {
        String key="PROCESSID";
        StringBuilder id=new StringBuilder();
        id.append("PROCESS");
        Calendar calendar=Calendar.getInstance();
        id.append(calendar.get(Calendar.YEAR)+""+(calendar.get(Calendar.MONTH)+1)+calendar.get(Calendar.DATE));
        Long idTemp=stringRedisTemplate.opsForValue().increment(key);
        String idStr= idTemp.toString();
        if (idStr.length()<5){
            for (int i=0;i<5-idStr.length();i++){
                idStr="0"+idStr;
            }
        }
        id.append(idStr);
        return MESResult.ok(id);
    }

    @Override
    public MESResult save(ProProcess proProcess) {
        ProProcess process = new ProProcess();
        process = proProcessMapper.selectByProcessName(proProcess);
       if (process!=null &&process.getProcessName().equals(proProcess.getProcessName())){
           return MESResult.duplicate();
       }
       else if (proProcessMapper.save(proProcess)==1)
       {
           return MESResult.ok();
       }
       else
       {
           return MESResult.duplicate();
       }
    }

    @Override
    public MESResult deleteProcess(String processCode) {
        if (proProcessMapper.deleteProcess(processCode)==1){
            return MESResult.ok();
        }
        else{
            return MESResult.fail();
        }
    }

    @Override
    public MESResult updateProcess(ProProcess process) {
        ProProcess process1 = new ProProcess();
        process1 = proProcessMapper.selectByProcessName(process);
        System.err.println(process);
        if (process1!=null &&process1.getProcessName().equals(process.getProcessName())){
            if (!process1.getProcessCode().equals(process.getProcessCode()))
            {
                return MESResult.duplicate();
            }
        }
        if (proProcessMapper.updateProcess(process)==1)
        {

            return MESResult.ok();
        }
        else
        {
            System.out.println("aaaaaaaaaaaaaaaaaaaaaaaaaa");
            return MESResult.fail();
        }
    }

    @Override
    public MESResult selectall() {
        return MESResult.ok(proProcessMapper.selectallByPage());
    }

}
